package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.article.entity.ApAuthor;
import com.heima.article.mapper.ApAuthorMapper;
import com.heima.article.service.IApAuthorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * APP文章作者信息表 服务实现类
 * </p>
 *
 * @author lqw
 * @since 2022-06-20
 */
@Service
public class ApAuthorServiceImpl extends ServiceImpl<ApAuthorMapper, ApAuthor> implements IApAuthorService {
    /**
     * 新增作者
     *
     * @param author
     * @return
     */
    @Override
    public ResponseResult<ApAuthor> saveApAuthor(ApAuthor author) {

        //保存作者
        if (author == null || author.getUserId() == null || author.getWmUserId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //判断是否已经有记录  判断user_id对应的记录
        LambdaQueryWrapper<ApAuthor> query = new LambdaQueryWrapper<>();
        //根据user_id查询
        query.eq(ApAuthor::getUserId, author.getUserId());
        ApAuthor one = this.getOne(query);
        //模拟报错
        //int a = 1/0;
        //如果存在作者,直接返回
        if (!StringUtils.isEmpty(one)) {
            return ResponseResult.okResult(one);
        }
        //新增,返回新增的作者
        this.save(author);
        return ResponseResult.okResult(author);
    }
}
